import { useGameStore } from '../../store/gameStore';

export default function GameControls() {
  const game = useGameStore(state => state.game);
  const ui = useGameStore(state => state.ui);
  const actions = useGameStore(state => state.actions);

  if (!game) return null;

  const currentPlayer = game.players[game.currentPlayerIndex];
  const currentSpace = game.board[currentPlayer.position];

  return (
    <div className="bg-white rounded-lg shadow-lg p-6 space-y-4">
      <h3 className="text-xl font-bold mb-4">游戏控制</h3>

      {/* 骰子结果 */}
      {ui.diceResult && (
        <div className="bg-purple-100 rounded-lg p-4 text-center">
          <p className="text-sm text-gray-600 mb-1">骰子结果</p>
          <p className="text-4xl font-bold text-purple-600">{ui.diceResult}</p>
        </div>
      )}

      {/* 当前位置信息 */}
      <div className="bg-gray-50 rounded-lg p-4">
        <p className="text-sm text-gray-600 mb-1">当前位置</p>
        <p className="font-bold">{currentSpace.name}</p>
        {currentSpace.property && (
          <div className="mt-2 text-sm">
            <p>价格: ${currentSpace.property.price}</p>
            {currentSpace.property.owner && (
              <p className="text-purple-600">
                所有者: {game.players.find(p => p.id === currentSpace.property?.owner)?.name}
              </p>
            )}
          </div>
        )}
      </div>

      {/* 操作按钮 */}
      <div className="space-y-2">
        <button
          onClick={() => actions.rollDice()}
          disabled={ui.isAnimating || ui.diceResult !== null}
          className="w-full bg-purple-600 text-white py-3 px-4 rounded-lg font-semibold hover:bg-purple-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors"
        >
          🎲 掷骰子
        </button>

        {currentSpace.property && !currentSpace.property.owner && (
          <button
            onClick={() => actions.buyProperty(currentSpace.property!.id)}
            disabled={currentPlayer.money < currentSpace.property.price}
            className="w-full bg-green-600 text-white py-3 px-4 rounded-lg font-semibold hover:bg-green-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors"
          >
            💰 购买地块
          </button>
        )}

        <button
          onClick={() => actions.endTurn()}
          disabled={ui.diceResult === null}
          className="w-full bg-blue-600 text-white py-3 px-4 rounded-lg font-semibold hover:bg-blue-700 disabled:bg-gray-300 disabled:cursor-not-allowed transition-colors"
        >
          ✓ 结束回合
        </button>

        <button
          onClick={() => actions.saveGame()}
          className="w-full bg-gray-600 text-white py-2 px-4 rounded-lg font-semibold hover:bg-gray-700 transition-colors"
        >
          💾 保存游戏
        </button>
      </div>

      {/* 能力按钮 */}
      <div className="border-t pt-4">
        <h4 className="font-semibold mb-2">角色能力</h4>
        <div className="space-y-2 text-sm">
          <div className="bg-blue-50 p-2 rounded">
            <p className="font-semibold text-blue-600">被动：{currentPlayer.character.passiveAbility.name}</p>
            <p className="text-gray-600 text-xs">{currentPlayer.character.passiveAbility.description}</p>
          </div>
          <div className="bg-purple-50 p-2 rounded">
            <p className="font-semibold text-purple-600">主动：{currentPlayer.character.activeAbility.name}</p>
            <p className="text-gray-600 text-xs">{currentPlayer.character.activeAbility.description}</p>
            <button
              onClick={() => actions.useAbility()}
              className="mt-2 w-full bg-purple-600 text-white py-1 px-2 rounded text-xs hover:bg-purple-700"
            >
              使用能力
            </button>
          </div>
        </div>
      </div>
    </div>
  );
}
